import request from '@/utils/request'
import axios from 'axios'
// 登录
export function loginAPI (data) {
  return request({ url: '/authorizations', method: 'post', data })
}

/**
 * 发送短信验证码
 */
export function sendVerify (mobile) {
  return request({ url: `/sms/codes/${mobile}` })
}

/**
* 关注 和 取消关注
* @param {*} authorId - 作者ID
* @param {*} isFollow - 是否关注
* @returns Promise
*/
export const followAuthor = (authorId, isFollow) => {
  // 关注
  if (!isFollow) {
    return request({
      url: '/user/followings',
      method: 'post',
      data: { target: authorId }
    })
  // 取消关注
  } else {
    return request({
      url: '/user/followings/' + authorId,
      method: 'delete'
    })
  }
}

/**
* 获取当前用户的信息（资料和统计）
*/
export const getUserInfo = () => {
  return request({ url: '/user' })
}

/**
* 获取当前用户的资料
*/
export const getUserProfile = () => {
  return request({ url: '/user/profile' })
}

/**
* 修改头像
* @param {Object} formData -  {photo:'文件数据'}
*/
export const updateUserPhoto = (formData) => {
  return request({
    url: '/user/photo',
    method: 'patch',
    data: formData
  })
}

/**
* 修改用户资料
* @param {Object} user - 用户对象
*/
export const updateUserProfile = (user) => {
  return request({
    url: '/user/profile',
    method: 'patch',
    data: user
  })
}

export function refreshToken (refreshToken) {
  return axios({
    url: process.env.VUE_APP_BASE_API + '/authorizations',
    method: 'put',
    headers: {
      Authorization: `Bearer ${refreshToken}`
    }
  })
}
